<!-- 会员中心主内容区 -->
<div class="page-article">
  <!-- 站内信：收件箱 -->
  <div id="member_messagebox" class="member-messagebox">
    <div class="member-tabs">
      <a href="<{link app=b2c ctl=site_member act=send}>"><em><{t}>发消息<{/t}></em></a>
      <a href="<{link app=b2c ctl=site_member act=inbox}>" class="active"><em><{t}>收件箱<{/t}><{if $inbox_num!=0}>(<span class="item-unread"><{$inbox_num}></span>)<{/if}></em></a>
      <a href="<{link app=b2c ctl=site_member act=track}>"><em><{t}>发件箱<{/t}></em></a>
      <a href="<{link app=b2c ctl=site_member act=outbox}>"><em><{t}>草稿箱<{/t}><{if $outbox_num!=0}>(<span class="item-unread"><{$outbox_num}></span>)<{/if}></em></a>
      <a href="<{link app=b2c ctl=site_member act=message}>"><em><{t}>给管理员发消息<{/t}></em></a>
    </div>
    <div class="member-mod member-messagebox-list">
      <table class="member-grid">
        <!-- 标题 -->
        <caption>
          <span class="messagebox-action"><a href="<{link app=b2c ctl=site_member full=1 act=deleteMsg}>" class="btn btn-simple action-batch-delete"><span><span><{t}>删除<{/t}></span></span></a></span>
          <span class="messagebox-tip"><{t}>共<{/t}><i><{$total_msg}></i><{t}>条消息<{/t}>，<b class="item-unread"><{$inbox_num}></b> <{t}>条未读<{/t}></span>
        </caption>
        <thead>
          <tr>
            <th class="check"><input type="checkbox" name="" id="" class="x-check action-check-all"></th>
            <th><{t}>发件人<{/t}></th>
            <th><{t}>标题<{/t}></th>
            <th><{t}>时间<{/t}></th>
            <th><{t}>操作<{/t}></th>
          </tr>
        </thead>
        <{if count($message)>0}>
        <{foreach item=msg from=$message name=name}>
        <tbody<{if $smarty.foreach.name.iteration==1}> class="first"<{/if}>>
          <tr class="handle action-view-detail <{if $smarty.foreach.name.iteration==1}> first<{/if}><{if $msg.mem_read_status == 'false'}> new<{/if}>">
            <td class="check"><input type="checkbox" name="delete[]" value="<{$msg.comment_id}>" id="" class="x-check op-check-all"<{if $smarty.foreach.name.first}> vtype="requiredcustom" data-validatormsg="请先选择消息后再删除"<{/if}> autocomplete="off"></td>
            <td class="adressee"><{if $msg.to_type == 1}><{t}>管理员<{/t}><{else}><{$msg.author}><{/if}></td>
            <td class="title"><{$msg.title|nl2br}></td>
            <td class="time"><{$msg.time|cdate:SDATE_STIME}></td>
            <td class="action"><a href="javascript:void(0);"><{t}>回复<{/t}></a></td>
          </tr>
          <tr class="detail">
            <td colspan="5">
              <p class="detail-title"><{$msg.title|nl2br}></p>
              <p class="detail-content"><{$msg.comment}></p>
              <div class="detail-reply">
                <form action="<{link app=b2c ctl=site_member act=send_msg}>" method="post" class="action-reply-form">
                  <ul>
                    <input type="hidden" name='msg_to' value="<{$msg.author}>">
                    <li class="form-item"><label for="" class="form-label"><{t}>收件人<{/t}>：</label><span class="form-act"><{$msg.author}></span></li>
                    <li class="form-item"><label for="for_msg_title" class="form-label"><{t}>标题<{/t}>：</label><span class="form-act"><input type="text" name="subject" id="for_msg_title" class="x-input" value="Re:<{$msg.title}>" vtype="required"></span></li>
                    <li class="form-item"><label for="for_msg_content" class="form-label"><{t}>内容<{/t}>：</label><span class="form-act"><textarea name="comment" id="for_msg_content" class="x-input" cols="50" rows="5" vtype="required"></textarea></span></li>
                    <li class="form-item">
                      <span class="form-act">
                        <button type="submit" class="btn btn-caution action-sendmsg" rel="_request"><span><span><{t}>发送<{/t}></span></span></button>
                        <button type="submit" class="btn btn-simple action-savetodrafts" name="has_sent" value="false" rel="_request"><span><span><{t}>保存到草稿箱<{/t}></span></span></button>
                      </span>
                    </li>
                  </ul>
                </form>
              </div>
            </td>
          </tr>
        </tbody>
        <{/foreach}>
        <{else}>
        <tbody>
          <tr class="first"><td colspan="5" class="no-information"><{t}>暂无消息<{/t}></td></tr>
        </tbody>
        <{/if}>
      </table>
    </div>
    <{pagers data=$pager}>
  </div>
</div>

<{if count($message)>0}>
<script>
var Module = $('member_messagebox');
var Router = {
  clean: '<{link app=b2c ctl=site_member act=view_msg}>'
};
var Query = {
    options: function(url, data, method, fn){
        return {
            url: url,
            method: method,
            data: data,
            link: 'cancel',
            onSuccess: function(rs) {
                try {
                    rs = JSON.decode(rs);
                }catch(e) {}
                if(typeOf(fn) === 'function') fn.call(this,rs);
            }
        };
    },
    send: function(url, data, fn) {
        new Request(Query.options(url,data, 'post', fn)).send();
    }
}

Object.merge(validatorMap, {
    requiredcustom: function(element, v, type, parent){
        var name = element.name;
        if(!parent.getElements('input[type=' + type + ']' + name ? '[name="' + name + '"]' : '').some(function(el) {
            element.bindEvent = false;
            return el.checked;
        })) {
            return Message.error(element.get('data-validatormsg'));
        }
        return true;
    }
});

Module.addEvents({
    'change:relay(.action-check-all)': function(e) {
        checkAll(this, Module.getElements('.op-check-all'));
    },
    'click:relay(.action-view-detail)': function (e) {
        if(e.target.tagName == 'INPUT') return;
        var tbody = this.getParent('tbody');
        var siblings = tbody.getSiblings('.active')[0];
        var parent = this;
        var next = parent.getNext('tr');
        if(parent.hasClass('active-top')) {
            parent.removeClass('active-top');
            next.removeClass('active-bottom');
            tbody.removeClass('active');
        }
        else {
            parent.addClass('active-top');
            next.addClass('active-bottom');
            tbody.addClass('active');
            cleanNew.delay(2000, this, [parent]);
            if(siblings) {
                siblings.removeClass('active').getElements('tr').removeClass('active-top').removeClass('active-bottom');
            }
        }
    },
    'click:relay(.action-batch-delete)': function(e) {
        e.stop();
        var parent = this.getParent('.member-mod');
        var data = [];
        parent.getElements('.handle').each(function(el){
            data.push(el.toQueryString());
        });
        data.push('response_json=true');
        data = data.join('&');

        if(validate(parent.getElement('input[vtype=requiredcustom]'), null, parent)) {
            Dialog.confirm('确定要删除消息吗？', function(e){
                if(e) async(this, {
                    url: this.href,
                    data: data,
                    method: 'post'
                });
            }.bind(this));
        }
    },
    'click:relay(.action-sendmsg)': function (e) {
        var hidden = $('action_submit_save');
        if(hidden) hidden.value = '';
    },
    'click:relay(.action-savetodrafts)': function (e) {
        $('action_submit_save') || new Element('input#action_submit_save[type=hidden]', {name: this.name, value: getValue(this)}).inject(this, 'after');
    }
});
function cleanNew(el){
    if(el.hasClass('new')) {
        var id = el.getElement('input[type=checkbox]').value;
        Query.send(Router.clean, 'comment_id=' + id, function(rs){
            el.removeClass('new');
            Module.getElements('.item-unread').each(function(el){
                el.innerHTML -= 1;
            });
        });
    }
}
function getValue(el) {
    /* <ltIE9> */
    var pollutesGetAttribute = (function(div){
      div.random = 'attribute';
      return (div.getAttribute('random') == 'attribute');
    })(document.createElement('div'));
    /* </ltIE9> */
    return pollutesGetAttribute ? el.getAttributeNode('value').nodeValue : el.get('value');
}
</script>
<{/if}>
